﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace HW6_1SimpleFilebrowser
{
    public partial class Form1 : Form
    {
        private string getpath, clickpath;
        public Form1()
        {
            InitializeComponent();
        }

        private void 文件FToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
        }

        private void toolStripTextBox1_Click_2(object sender, EventArgs e)
        {
            if (textBox1.Text == string.Empty)
            {
                MessageBox.Show("  您还未选择路径  " + "\r\n" + "  请选择文件夹路径  ");
            }
        }
        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog dialog  = new System.Windows.Forms.FolderBrowserDialog(); //定义打开新的文件夹
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                textBox1.Text = dialog.SelectedPath;
            }
            getpath = textBox1.Text;
            CreateDirTree();
        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            
        }

       
        private void CreateDirTree()
        {
            treeView1.Nodes.Clear();
            TreeNode root = new TreeNode(getpath);
            root.Tag = getpath;
            DirectoryInfo dirInfo = new DirectoryInfo(getpath);//用于查询目录,获得程序所在路径的目录对像
            DirectoryInfo[] dirs = dirInfo.GetDirectories();//获得目录下文件夹对象
            int dircount = dirs.Count();//获得文件夹对象数量
            CreateDirec(root, dirInfo.GetDirectories());
            treeView1.Nodes.Add(root);
            root.Expand();
        }

        private void CreateDirec(TreeNode parentnode, DirectoryInfo[] directoryInfos)
        {
            foreach (DirectoryInfo dir in directoryInfos)
            {
                try
                {
                    TreeNode subtn = new TreeNode(dir.Name);
                    subtn.Text = dir.Name;
                    subtn.Tag = dir.FullName;//设置TAG属性为完整的文件夹地址
                    if (dir.GetDirectories().Count() > 0)
                    {
                        CreateDirec(subtn, dir.GetDirectories());
                    }
                    parentnode.Nodes.Add(subtn);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("发生错误" + ex.Message);
                }
            }
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            clickpath = e.Node.Tag as string;
            if(clickpath != null)
            ShowFile(clickpath);
        }

        private void ShowFile(string clickpath)
        {
            listView1.Items.Clear();

            // 显示子文件夹
            DirectoryInfo dirIns = new DirectoryInfo(clickpath);
            try
            {
            foreach (DirectoryInfo dir in dirIns.GetDirectories())
            {
                ListViewItem item = new ListViewItem(dir.Name, 0);
                item.Tag = dir.FullName;
                listView1.Items.Add(item);
            }
            }
            catch(Exception ex)
            {
                MessageBox.Show("发生错误" + ex.Message);
            }
            // 显示文件
            try
            {
                foreach (FileInfo file in dirIns.GetFiles())
                {
                    if (file.Extension.ToLower() == ".txt")
                    {
                        ListViewItem item = new ListViewItem(file.Name, 1);
                        item.Tag = file.FullName;
                        listView1.Items.Add(item);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("发生错误" + ex.Message);
            }
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                string filePath = listView1.SelectedItems[0].Tag.ToString();
                FileInfo file = new FileInfo(filePath);
                if(file.Extension.ToLower() == ".txt")
                {
                    System.Diagnostics.Process.Start(filePath);
                }
            }
        }

        private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {

        }

    }
}

